#include <game/controls/Button.h>
#include <game/input/TouchEvent.h>
#include <Object2D.h>
#include <math/Vector2.h>
#include <math/Rect.h>
#include <logs/Log.h>

namespace {
const char* LOGGER="Button";
} // anonymous namespace

ARELLO_GAME_NAMESPACE_DECL_START
namespace controls {


ButtonLoader::ButtonLoader(Button *btn)
	: btn(btn)
{
}

bool ButtonLoader::on_node(xml::Provider * prov, const std::string & name)
{
	return false;
}


boost::shared_ptr<Control> Button::Creator(const liba::Atom& name, liba::Object2D* o2d)
{
	return boost::shared_ptr<Control>(new Button(name, o2d));
}

Button::Button(const liba::Atom& name, liba::Object2D* o2d)
	: Control(name, o2d)
	, loader(this)
{
}

InputSharingMode Button::GetSharingMode()
{ 
	return INPUT_SHARE_CONTINUE;
}

void Button::OnTouchStarted(const game::input::TouchEvent& ev)
{
	LOG_INFO(LOGGER, "OnTouchStarted: Global (%f, %f) Local (%f, %f)", 
		ev.GlobalPoint.x, ev.GlobalPoint.y, ev.LocalPoint.x, ev.LocalPoint.y);
}

void Button::OnTouchMove(const game::input::TouchEvent& ev)
{
	LOG_INFO(LOGGER, "OnTouchMove: Global (%f, %f) Local (%f, %f)", 
		ev.GlobalPoint.x, ev.GlobalPoint.y, ev.LocalPoint.x, ev.LocalPoint.y);
}

void Button::OnTouchFinished(const game::input::TouchEvent& ev)
{
	LOG_INFO(LOGGER, "OnTouchFinished: Global (%f, %f) Local (%f, %f)", 
		ev.GlobalPoint.x, ev.GlobalPoint.y, ev.LocalPoint.x, ev.LocalPoint.y);

	math::Rect<float> bound = GetObject2D()->GetGlobalBounds();
	if (bound.is_intersects(ev.GlobalPoint))
		click(this);
}

} // namespace controls
ARELLO_GAME_NAMESPACE_DECL_END 
